from utils.sqlutils import SqlUtils
from sqlmap.usersqlmap import SqlMap as UserSqlMap


class User:
    def __init__(self):
        self.sqlutils = SqlUtils()

    def user_list(self):
        data = self.sqlutils.execute(UserSqlMap.get("list"))
        user_list = []
        for row in data.get("content"):
            user = {}
            user["id"] = row[0]
            user["name"] = row[1]
            user_list.append(user)
        return user_list

    def user_info(self,**kwargs):
        data = self.sqlutils.execute(UserSqlMap.get("info"))
        user = {}
        for row in data.get("content"):
            user["id"] = row[0]
            user["name"] = row[1]
        return user
    
    def user_info_name(self,**kwargs):
        data = self.sqlutils.execute(UserSqlMap.get("get_name").format(**kwargs))
        user = {}
        for row in data.get("content"):
            user["id"] = row[0]
            user["name"] = row[1]
        return user

    def user_create(self, **kwargs):
        if self.sqlutils.find(UserSqlMap.get("get_name").format(**kwargs)).get("count")>0:
            return None
        return self.sqlutils.execute(UserSqlMap.get("create").format(**kwargs))

    def user_delete(self, **kwargs):
        return self.sqlutils.execute(UserSqlMap.get("delete").format(**kwargs))

    def user_edit(self, **kwargs):
        return self.sqlutils.execute(UserSqlMap.get("edit").format(**kwargs))


if __name__ == '__main__':
    user = User()
    user.user_create(name="mx")
    # print(user.user_list())
